﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Common;
using SYH.Config;

namespace SYH.Purchase.DAL
{
    public class Purchase_Info
    {
        #region 判断该用户的此采购标题是否重复 返回0重复 1不重复
        /// <summary>
        /// 判断该用户是否已存在此求购标题，避免重复
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public int GetIsHasTitle(int userId, string title)
        {
            SqlParameter outParam = new SqlParameter("@Result", SqlDbType.Int, 4);
            outParam.Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(
              WebConfig.SqlConnString,
              CommandType.StoredProcedure,
              "Purchase_Info_TitleExists",
              new SqlParameter("@UserId", userId),
              new SqlParameter("@Title", title),
              new SqlParameter("@Result", title),
              outParam
              );
            return TypeParse.ObjectToInt(outParam, 0);
        }
        #endregion

        #region 添加采购信息
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(SYH.Purchase.Model.Purchase_Info model, string oftenText, string oftenValue, string imgNameList, string imgIdList)//
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@BuyCount", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
                    new SqlParameter("@Summary",SqlDbType.NVarChar,300),
					new SqlParameter("@Content", SqlDbType.NVarChar),
					new SqlParameter("@Standard", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.Int,4),
					new SqlParameter("@Coverage", SqlDbType.VarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Validity", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@IsDelete", SqlDbType.Bit,1),
                    new SqlParameter("@ImgUrl",SqlDbType.VarChar,100),
                    new SqlParameter("@UpdateTime",SqlDbType.DateTime),
                    new SqlParameter("@EndTime",SqlDbType.DateTime),
                    new SqlParameter("@ProductName",SqlDbType.NVarChar,100),
                    new SqlParameter("@Address",SqlDbType.NVarChar,200),
                    new SqlParameter("@OftenText", SqlDbType.NVarChar,50),
                    new SqlParameter("@OftenValue", SqlDbType.VarChar,50),
                    new SqlParameter("@ImgNameList", SqlDbType.VarChar,200),
                    new SqlParameter("@imgIdList", SqlDbType.VarChar,200), 
                    new SqlParameter("@flag", SqlDbType.Int,4),
					new SqlParameter("@Unit", SqlDbType.NVarChar,10)
                                        };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.BuyCount;
            parameters[3].Value = model.Price;
            parameters[4].Value = model.Summary;
            parameters[5].Value = model.Contnet;
            parameters[6].Value = model.Standard;
            parameters[7].Value = model.Type;
            parameters[8].Value = model.Coverage;
            parameters[9].Value = model.Status;
            parameters[10].Value = model.UserId;
            parameters[11].Value = model.Validity;
            parameters[12].Value = model.CreateTime;
            parameters[13].Value = model.IsDelete;
            parameters[14].Value = model.ImgUrl;
            parameters[15].Value = model.UpdateTime;
            parameters[16].Value = model.EndTime;
            parameters[17].Value = model.ProductName;
            parameters[18].Value = model.Address;
            parameters[19].Value = oftenText;
            parameters[20].Value = oftenValue;
            parameters[21].Value = imgNameList;
            parameters[22].Value = imgIdList;
            parameters[23].Value = model.Flag;
            parameters[24].Value = model.Unit;
            Common.SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.StoredProcedure, "Purchase_Info_Add", parameters);

            return (int)parameters[0].Value;
        }
        #endregion

        #region 根据Id得到一个对象实体
        /// <summary>
        /// 得到一个对象实体 根据主键
        /// </summary>
        /// <param name="Id">主键</param>
        /// <returns></returns>
        public Model.Purchase_Info GetModel(int Id)
        {
            SqlParameter[] par = new SqlParameter[] { 
                new SqlParameter("@Id",Id)
            };

            DataTable dt = Common.SqlHelper.ExecuteDataset(WebConfig.SqlConnString, CommandType.StoredProcedure, "Purchase_Info_GetModel", par).Tables[0];
            if (dt.Rows.Count > 0)
            {
                SYH.Purchase.Model.Purchase_Info model = new Model.Purchase_Info();
                model.ProductName = dt.Rows[0]["ProductName"].ToString();
                model.Title = dt.Rows[0]["Title"].ToString();
                model.BuyCount = Common.TypeParse.ObjectToInt(dt.Rows[0]["BuyCount"], 0);
                model.Price = Common.TypeParse.ObjectToDecimal(dt.Rows[0]["Price"], 0);
                model.Standard = dt.Rows[0]["Standard"].ToString();
                model.Contnet = dt.Rows[0]["Contnet"].ToString();
                model.Type = Common.TypeParse.ObjectToInt(dt.Rows[0]["Type"].ToString(), 0);
                model.ImgUrl = dt.Rows[0]["Imgurl"].ToString();
                model.Address = dt.Rows[0]["Address"].ToString();
                model.ViewCount = Common.TypeParse.ObjectToInt(dt.Rows[0]["ViewCount"], 0);
                model.Validity = Common.TypeParse.ObjectToInt(dt.Rows[0]["Validity"].ToString(), 0);
                model.CreateTime = Common.TypeParse.ObjectToDateTime(dt.Rows[0]["CreateTime"].ToString(), DateTime.Now);
                model.UserId = Common.TypeParse.ObjectToInt(dt.Rows[0]["UserId"], 0);
                return model;
            }
            else
            {
                return null;
            }
        }
        #endregion
      
        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDs">ID集合</param>
        /// <returns></returns>
        public int DeleteIdList(string IDs)
        {
            return Common.SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, "update Purchase_Info set  Status=" + (int)SYH.Enum.PurchaseInfoverifyType.已删除 + " where Id in (" + IDs + ")");
        }
        #endregion

        #region 旺铺后台修改采购信息
        /// <summary>
        /// 修改采购信息
        /// </summary>
        public bool Update(Model.Purchase_Info model, string oftenText, string oftenValue, string imgNameList, string imgIdList)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@BuyCount", SqlDbType.Int,4),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Content", SqlDbType.NVarChar),
					new SqlParameter("@Standard", SqlDbType.NVarChar,50),
					new SqlParameter("@Type", SqlDbType.Int,4),
					new SqlParameter("@Coverage", SqlDbType.VarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Validity", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@IsDelete", SqlDbType.Bit,1),
                    new SqlParameter("@ImgUrl",SqlDbType.VarChar,100),
                    new SqlParameter("@UpdateTime",SqlDbType.DateTime),
                    new SqlParameter("@EndTime",SqlDbType.DateTime),
                    new SqlParameter("@ProductName",SqlDbType.NVarChar,100),
                    new SqlParameter("@Address",SqlDbType.NVarChar,200),
                    new SqlParameter("@OftenText", SqlDbType.NVarChar,50),
                    new SqlParameter("@OftenValue", SqlDbType.VarChar,50),
                    new SqlParameter("@ImgNameList", SqlDbType.VarChar,200),
                    new SqlParameter("@imgIdList", SqlDbType.VarChar,200),
					new SqlParameter("@Unit", SqlDbType.NVarChar,10),
                    new SqlParameter("@Summary",SqlDbType.NVarChar,300)
                                        };
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.BuyCount;
            parameters[3].Value = model.Price;
            parameters[4].Value = model.Contnet;
            parameters[5].Value = model.Standard;
            parameters[6].Value = model.Type;
            parameters[7].Value = model.Coverage;
            parameters[8].Value = model.Status;
            parameters[9].Value = model.UserId;
            parameters[10].Value = model.Validity;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.IsDelete;
            parameters[13].Value = model.ImgUrl;
            parameters[14].Value = model.UpdateTime;
            parameters[15].Value = model.EndTime;
            parameters[16].Value = model.ProductName;
            parameters[17].Value = model.Address;
            parameters[18].Value = oftenText;
            parameters[19].Value = oftenValue;
            parameters[20].Value = imgNameList;
            parameters[21].Value = imgIdList;
            parameters[22].Value = model.Unit;
            parameters[23].Value = model.Summary;
            return Common.SqlHelper.ExecuteNonQuery(WebConfig.SqlConnString, CommandType.StoredProcedure, "Purchase_Info_Update", parameters) > 0;
        }
        #endregion

        #region 查询的采购信息列表
        /// <summary>
        /// 查询我的采购信息 (默认安发布时间排序 CreateTime)
        /// </summary>
        /// <param name="pageSize">页面大小</param>
        /// <param name="pageIndex">当前页面</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="orderType">排序方式</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="recordCount">总记录数</param>
        /// <param name="userId">用户Id</param>
        /// <param name="status">采购信息状态</param>
        /// <returns></returns>
        public DataSet GetPurchaseList(int pageSize, int pageIndex, string orderField, bool orderType, out int pageCount, out int recordCount, int userId, int status)
        {
            string strWhere = " and po.UserId=" + userId + "  and  pa.DataType=1 and pa.IndustryLevel=3 ";
            switch (status)
            {
                case (int)SYH.Enum.PurchaseInfoverifyType.审核中:
                    strWhere += " and Status=" + (int)SYH.Enum.PurchaseInfoverifyType.审核中;
                    break;
                case (int)SYH.Enum.PurchaseInfoverifyType.未通过:
                    strWhere += " and Status=" + (int)SYH.Enum.PurchaseInfoverifyType.未通过;
                    break;
                case (int)SYH.Enum.PurchaseInfoverifyType.已过期:
                    strWhere += " and EndTime<'" + DateTime.Now + "' and Status in(" +
                                (int)SYH.Enum.PurchaseInfoverifyType.已过期 + " ," +
                                (int)SYH.Enum.PurchaseInfoverifyType.正常 + ")";
                    break;
                case (int)SYH.Enum.PurchaseInfoverifyType.已删除:
                    strWhere += " and Status=" + (int)SYH.Enum.PurchaseInfoverifyType.已删除;
                    break;
                case (int)SYH.Enum.PurchaseInfoverifyType.正常:
                    strWhere += " and Status=" + (int)SYH.Enum.PurchaseInfoverifyType.正常;
                    break;
            }
            return Common.PublicSqlData.SimplePager(
                           WebConfig.SqlConnString,
                          "Purchase_Info as po inner join Public_IndustryData as pa on po.Id=pa.DataId  ",
"po.Title,po.Id,po.ImgUrl,po.ProductName,po.CreateTime,po.UpdateTime ,po.Summary,Pa.IndustryId,po.Status",
                           pageSize,
                           pageIndex,
                           orderField,
                           orderType,
                           strWhere,
                           out pageCount,
                           out recordCount
                          );
        }
        #endregion
    }
}
